package com.sunfeng.stack;

/**
 * 用数组模拟栈
 *
 * @author sunfeng
 * @date 2021/04/25 09:32
 **/
public class ArrayStackDemo {
    public static void main(String[] args) {
        ArrayStack arrayStack = new ArrayStack(10);
        arrayStack.push(1);
        arrayStack.push(2);
        arrayStack.push(3);
        arrayStack.push(3);
        arrayStack.list();
    }

}

class ArrayStack {
    /**
     * 栈的大小
     */
    private int maxSize;
    /**
     * 数据存储数据
     */
    private int[] stack;
    /**
     *
     */
    private int top;

    public ArrayStack(int maxSize) {
        this.maxSize = maxSize;
        this.stack = new int[this.maxSize];
    }

    public boolean isFull() {
        return top == maxSize - 1;
    }

    public boolean isEmpty() {
        return top == -1;
    }

    /**
     * 入栈
     *
     * @param data
     */
    public void push(int data) {
        if (isFull()) {
            System.out.println("栈已经满了");
            return;
        }
        top++;
        stack[top] = data;
    }


    public int pop() {
        if (isEmpty()) {

            throw new RuntimeException("栈已经空了了");
        }
        int value = stack[top];
        top--;
        return value;
    }

    public void list() {
        if (isEmpty()) {
            System.out.println("栈已经空了了");
            return;
        }
        for (int i = top; i >=0 ; i--) {
            System.out.println(stack[ i]);
        }

    }
}